<?php
class api extends core
{   
    public function __construct()
    {
        parent::__construct();
        $this->member_mod=m('member');
    }
    //error错误类型提示-1：账号不存在，-2：密码不正确，-3：转出数量必须为100的整数倍，
    //                 -4：转出理财积分应大于余额理财积分，-5：转入失败
    //                 -6：转出失败，-7：数据错误，-8：请提供订单号，-10：其他
    //type调用类型1：转入，2：转出

    // 返回的数据：$res = array(
    //     "result_code" => 0, //<0，失败，>=0 成功
    //     "result_msg" => "成功",
    //     "order_sn" => "" //数商交易所的订单号
    //     "merchant_order_sn" => "", //第三方商户平台的订单号
    //     "integral" => "",
    //     "name" => "",
    // );
    //api_return($result_code=0,$result_msg,$order_sn,$merchant_order_sn,$integral,$name)
    //$result_code 状态码
    //$result_msg 提示文字
    //$order_sn 第三方订单号
    //$merchant_order_sn 本系统订单号
    //$integral 数量
    //$name 账号
    //接收数据
    // name={$name}
    // password={$password}
    // integral={$integral}
    // order_sn={$order_sn}
    // sign={$sign}
    // api_return($result_code=0,$result_msg='',$order_sn='',$merchant_order_sn='',$integral=0,$sign='')
    //转入
    public function transfer_in(){
        $data = isset($_POST['data'])&&trim($_POST['data'])?trim($_POST['data']):"";
        write_log($data);
        $pu_key = openssl_pkey_get_public($this->transfer_platforms[1]['public_key']);
		$crypto = '';
		foreach (str_split(base64_decode($data), 128) as $chunk) {
			openssl_public_decrypt($chunk, $decryptData, $pu_key);//$decryptData为解密后的数据
			$crypto .= $decryptData;
		}
        write_log($crypto);
        // $crypto = '{"name":"13533565425","password":"000000","integral":1,"order_sn":"178118003747787","sign":"9716ab0a0df858586ab95f51fe45d602"}';
        $getdata = json_decode($crypto);

		// var_dump($this->transfer_platforms[1]['public_key']);
        if(!$getdata) api_return('-7','数据错误');
        if(!$getdata->order_sn) api_return('-8','请提供订单号',$getdata->order_sn,'',$getdata->integral,$getdata->sign);
        $getdata->integral = floatval($getdata->integral);
        if ($getdata->integral<=0 || ceil($getdata->integral)!=$getdata->integral) api_return('-3','转账积分不正确，如不能为小数',$getdata->order_sn,'',$getdata->integral,$getdata->sign);
        $getdata->integral = intval($getdata->integral);

        $sql = "SELECT * FROM jf_member WHERE mobile='{$getdata->name}'";
        $user = $this->db->get_row($sql);

        if(!$user || !$getdata->name) api_return('-1','账号不存在',$getdata->order_sn,'',$getdata->integral,$getdata->sign);

        $password=crypt_password($getdata->password,$user->salt);
        if($password != $user->password) api_return('-2','密码不正确',$getdata->order_sn,'',$getdata->integral,$getdata->sign);

        $merchant_order_sn = generate_order_sn();
        try{
            // 开启事务
            $this->db->query("START TRANSACTION");
            //更新member，理财积分
            $sql = "UPDATE jf_member SET integral = integral +'{$getdata->integral}'  WHERE id='{$user->id}'";
            $this->db->query($sql);
            //写入明细表
            $data=array(
                'integral'=>$getdata->integral,
                'type'=>1,
                'member_id'=>$user->id,
                'add_time'=>time(),
                'memo'=> $getdata->order_sn,
                'order_sn'=> $merchant_order_sn
            );
            // DB::from("{$this->tbp}member_integral_history")->insert($data);
            $sql = "INSERT INTO jf_member_integral_history(integral, type, member_id, add_time, memo, order_sn) 
                VALUES ('{$getdata->integral}', '1', '{$user->id}', '".time()."','{$getdata->order_sn}','{$merchant_order_sn}')";
            // var_dump($sql);
            $this->db->query($sql);
            $this->db->query("COMMIT");
            api_return('0','转入成功',$getdata->order_sn,$merchant_order_sn,$getdata->integral,$getdata->sign);
        }catch(Exception $e){
            $this->db->query("ROLLBACK");
            api_return('-6','转入失败',$getdata->order_sn,$merchant_order_sn,$getdata->integral,$getdata->sign);
        }   
    } 

}
